<?php


namespace app\common\model\goods;


use think\Model;

class GoodsSearchLogModel extends Model
{
    protected $name = 'goods_search_log';

    /**
     * 添加搜索历史
    */
    public static function addLog($user_id, $search)
    {
        return self::create([
            'user_id' => $user_id,
            'title' => $search,
            'createtime' => time(),
        ]);
    }

    /**
     * 清空搜索历史
    */
    public static function clearSearchLog($user_id)
    {
        try {
            self::where(['user_id' => $user_id,])->delete();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 获取搜索历史
    */
    public function getSearchList($user_id): array
    {
        try {
            $list = $this
                ->where([
                    'user_id' => $user_id
                ])
                ->order('createtime','desc')
                ->limit(10)
                ->select();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

}